package authorizedDao

import (
	"gitee.com/yangguoliang001/go-gin-vue3/BO"
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/model"
)

func (a *authorizedDao) QueryAuthApiDetail(apiReq QO.AddAuthApiReq) (apiItem model.AuthorizedApiDO, err error) {
	err = a.db.Debug().Table("authorized_api").Where("business_key = ? AND method = ? AND api = ?", apiReq.BusinessKey, apiReq.Method, apiReq.Api).First(&apiItem).Error
	return apiItem, err
}

func (a *authorizedDao) QueryMethodList() (methodList []BO.MethodBO, err error) {
	err = a.db.Debug().Table("authorized_api").Select("distinct method").Find(&methodList).Error
	return
}

func (a *authorizedDao) QueryAuthApiList(businessKey string) (apiList []model.AuthorizedApiDO, err error) {
	err = a.db.Debug().Table("authorized_api").Where("business_key = ? AND is_deleted = ?", businessKey, -1).Find(&apiList).Error
	return
}

func (a *authorizedDao) AddAuthorizedApi(apiReq model.AuthorizedApiDO) (apiItem model.AuthorizedApiDO, err error) {
	// 如果存在 返回 获取到的结果，如果不存在 创建成功后返回创建的结果
	err = a.db.Debug().Table("authorized_api").FirstOrCreate(&apiReq, "business_key = ? AND method = ? AND api = ?", apiReq.BusinessKey, apiReq.Method, apiReq.Api).Error
	return apiReq, err
}

func (a *authorizedDao) DeleteAuthorizedApi(id string) (err error) {
	err = a.db.Debug().Table("authorized_api").Where("id = ?", id).Update("is_deleted", 1).Error
	return
}
